package com.xingfudazi.app.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingfudazi.app.domain.AppAccount;
import com.xingfudazi.app.domain.AppRoleApply;
import com.xingfudazi.app.domain.ImUser;
import com.xingfudazi.app.mapper.AppAccountMapper;
import com.xingfudazi.app.service.IAppAccountService;
import com.xingfudazi.app.service.ImUserService;
import com.xingfudazi.common.enums.DBDataStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账号Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppAccountServiceImpl implements IAppAccountService
{
    @Autowired
    private AppAccountMapper appAccountMapper;

    @Autowired
    private ImUserService imUserService;

    /**
     * 查询账号
     * 
     * @param id 账号主键
     * @return 账号
     */
    @Override
    public AppAccount selectAppAccountById(Long id)
    {
        return appAccountMapper.selectById(id);
    }

    /**
     * 查询账号列表
     * 
     * @param appAccount 账号
     * @return 账号
     */
    @Override
    public List<AppAccount> selectAppAccountList(AppAccount appAccount)
    {
        LambdaQueryWrapper<AppAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppAccount::getCreateTime);
        return appAccountMapper.selectList(null);
    }

    /**
     * 新增账号
     * 
     * @param appAccount 账号
     * @return 结果
     */
    @Override
    public int insertAppAccount(AppAccount appAccount)
    {
        return appAccountMapper.insert(appAccount);
    }

    /**
     * 修改账号
     * 
     * @param appAccount 账号
     * @return 结果
     */
    @Override
    public int updateAppAccount(AppAccount appAccount)
    {
        return appAccountMapper.updateById(appAccount);
    }

    /**
     * 批量删除账号
     * 
     * @param ids 需要删除的账号主键
     * @return 结果
     */
    @Override
    public int deleteAppAccountByIds(Long[] ids)
    {
        LambdaQueryWrapper<AppAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AppAccount::getId, (Object[]) ids);
        wrapper.eq(AppAccount::getDelFlag, DBDataStatus.DELETED.getCode());

        AppAccount appAccount = new AppAccount();
        appAccount.setDelFlag(DBDataStatus.DELETED.getCode());
        return appAccountMapper.update(appAccount,wrapper);
    }

    /**
     * 删除账号信息
     * 
     * @param id 账号主键
     * @return 结果
     */
    @Override
    public int deleteAppAccountById(Long id)
    {
        AppAccount appAccount = new AppAccount();
        appAccount.setDelFlag(DBDataStatus.DELETED.getCode());
        appAccount.setId(id);
        return appAccountMapper.updateById(appAccount);
    }

    @Override
    public AppAccount selectByUsername(String username) {
        LambdaQueryWrapper<AppAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppAccount::getUsername,username);
        return  this.appAccountMapper.selectOne(wrapper);
    }
}
